<template>
	<component
		ref="wyObject"
		:roomList="roomList"
		:is="Form"
		:formData="wyform"
		:rules="rules"
		:bedList="bedList"
		:save="save"
		:getBedList="getBedList"
		:bloodTypeList="bloodTypeList"></component>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { post, get } from '@/axios'
import { ElMessage } from 'element-plus'
import Form from './components/form'
import roomapi from '@/api/roomapi'
const props = defineProps(['getTable', 'urls', 'id'])
const emits = defineEmits(['update:show'])
const wyObject = ref()
const roomList = ref([])
const bedList = ref([])
const bloodTypeList = [
	{
		text: 'A型',
	},
	{
		text: 'B型',
	},
	{
		text: 'O型',
	},
	{
		text: 'AB型',
	},
	{
		text: '其他',
	}
]
const wyform = reactive({
	name: '',
	idcard: '',
	roomNo: '',
	checkinDate: '',
	expirationDate: '',
	contactTel: '',
	bedId: null,
	psychosomaticState: '',
	attention: '',
	height: '',
	weight: '',
	bloodType: '',
	familyMember: ''

})
const rules = {
	name: [
		{ required: true, message: '请输入客户姓名', trigger: 'blur' },
		{ max: 20, message: '客户姓名最多20个字符', trigger: 'blur' }
	],
	idcard: [
		{ required: true, message: '请输入客户身份证号', trigger: 'blur' },
		{ pattern: /^\d{6}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/, message: '请输入正确的身份证号', trigger: 'blur'  }
	],
	roomNo: [
		{ required: true, message: '请选择房间号', trigger: 'blur' }
	],
	checkinDate: [
		{ required: true, message: '请选择入住日期', trigger: 'blur' }
	],
	expirationDate: [
		{ required: true, message: '请选择退住日期', trigger: 'blur' }
	],
	contactTel: [
		{ required: true, message: '请输入联系电话', trigger: 'blur' },
		{ pattern: /^1[3456789]\d{9}$/, message: '请输入正确的联系电话', trigger: 'blur' }
	],
	bedId: [
		{ required: true, message: '请选择床位号', trigger: 'blur' }
	],
	bloodType: [
		{ required: true, message: '请选择血型', trigger: 'blur' }
	],
	familyMember: [
		{ required: true, message: '请输入家庭成员信息', trigger: 'blur' }
	]
}
function getById() {
	return new Promise(resolve => {
		get(props.urls.getById, { id: props.id }, content => {
			resolve(content)
		})
	})
}
let form1 = {}
if (props.id) {
	form1 = await getById()
	for (const key in wyform) {
		wyform[key] = form1[key]
	}
	wyform.id = props.id
	wyform.roomNo = parseInt(wyform.roomNo)
	console.dir(form1)
}
roomList.value = await roomapi.getRooms(wyform.bedId)
getBedList(wyform.roomNo)
function getBedList(value) {
	wyform.bedId = null
	for (const i in roomList.value) {
		if (value === roomList.value[i].id) {
			bedList.value = roomList.value[i].beds
			return
		}
	}
}
wyform.bedId = form1.bedId
function save() {
	post(props.id ? props.urls.update : props.urls.add, wyform, content => {
		emits('update:show', false)
		props.getTable()
	}, wyObject.value.formObj)
}

</script>

<style scoped lang="scss">
</style>